package com.bwhx.eruler.controller.casdilly;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFDataFormat;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.bean.casdilly.CasdillyCustomer;
import com.bwhx.eruler.bean.casdilly.CasdillyScanData;
import com.bwhx.eruler.bean.casdilly.ClothesType;
import com.bwhx.eruler.bean.casdilly.ClothesTypeExport;
import com.bwhx.eruler.controller.BaseController;
import com.bwhx.eruler.service.StaffService;
import com.bwhx.eruler.service.casdilly.CasdillyCustomerService;
import com.bwhx.eruler.service.casdilly.CasdillyScanDataService;
import com.bwhx.eruler.service.casdilly.ClothesTypeService;
import com.bwhx.eruler.service.casdilly.IClothesTypeExportService;
import com.bwhx.eruler.util.StringUtils;

@Controller
@RequestMapping("/langdeng")
public class LangdengCustomerController extends BaseController {

	@Autowired
	CasdillyCustomerService customerService;

	@Autowired
	CasdillyScanDataService casdillyScanDataService;

	@Autowired
	ClothesTypeService clothesTypeService;

	@Autowired
	StaffService staffService;

	@Autowired
	IClothesTypeExportService clothesTypeExportService;

	String exportPath = "/home/bwhx/package/huamingcexls/";

//	String exportPath = "/Users/yang/home/bwhx/package/huamingcexls/";

	
	@ResponseBody
	@RequestMapping(value = "/page.langdeng.hodo_exportdata_body.ashx")
	public void HodoExportdataBody(CasdillyCustomer customer, HttpServletResponse response, HttpServletRequest request)
			throws FileNotFoundException, IOException {

		// 解决参数乱码问题
		String customerName = request.getParameter("customerName");
		if (!StringUtils.isEmpty(customerName)) {

			customerName = java.net.URLDecoder.decode(request.getParameter("customerName"), "UTF-8");
			customer.setCustomerName(customerName);
		}

		Staff sessionStaff = this.getSessionStaff();
		if (null == sessionStaff.getUserType() || 1 != sessionStaff.getUserType()) {
			customer.setCreatorID(sessionStaff.getsId().toString());
		}

		// 获取品类信息，成衣信息

		// 品类信息
		List<ClothesType> clothesTypeList = clothesTypeService.findByTypeCPIDSerial(customer.getHetongSerial());

		// 获取用户信息 和测量数据信息
		List<CasdillyCustomer> customerList = customerService.hodoListByHetongSerial(customer);
		// 初始化excel
		XSSFWorkbook initExcel = initBodyExcel(sessionStaff, clothesTypeList, customerList);

		// 获取图片资料并压缩

		// response 返回

		// ! 5、将Excel 保存到硬盘

		String filePath = null;
		String os = System.getProperty("os.name");
		if (os.toLowerCase().startsWith("win")) {
			filePath = String.format("%s/home/bwhx/package/huamingcexls/%s于%s导出%s.xlsx",
					StringUtils.GetRootPath(request), this.getSessionStaff().getsName(),
					new SimpleDateFormat("YYYY年MM月dd日HH时mm分").format(new Date()), customer.getHetongSerial());
		} else {
			filePath = String.format("%s/%s于%s导出%s.xlsx", exportPath, this.getSessionStaff().getsName(),
					new SimpleDateFormat("YYYY年MM月dd日HH时mm分").format(new Date()), customer.getHetongSerial());
		}
		FileOutputStream fileOutputStream = new FileOutputStream(filePath);

		initExcel.write(fileOutputStream);
		fileOutputStream.close();

		File file = new File(filePath);
		String filename = file.getName();
		// 以流的形式下载文件。
		InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
		byte[] buffer = new byte[fis.available()];
		fis.read(buffer);
		fis.close();
		final String userAgent = request.getHeader("USER-AGENT");
		if (userAgent.contains("Firefox")) {// google,火狐浏览器
			filename = new String(filename.getBytes(), "ISO8859-1");
		} else {
			filename = URLEncoder.encode(filename, "UTF-8");// 其他浏览器
		}
		response.reset();
		// 设置response的Header
		String string = new String(filename.getBytes());
		response.addHeader("Content-Disposition", String.format("attachment;filename=%s", string));
		response.addHeader("Content-Length", "" + file.length());
		OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		toClient.write(buffer);
		toClient.flush();
		toClient.close();
		file.delete();
	}

	/*
	 * 填充搜索的用户测量数据
	 */
	private XSSFWorkbook initBodyExcel(Staff staff, List<ClothesType> clothesTypeList,
			List<CasdillyCustomer> customerList) {
		XSSFWorkbook hssfWorkbook = new XSSFWorkbook();
		initTotleTable(staff, clothesTypeList, customerList, hssfWorkbook);
		List<CasdillyCustomer> manlist = new ArrayList<CasdillyCustomer>();
		List<CasdillyCustomer> womanlist = new ArrayList<CasdillyCustomer>();
		for (int i = 0; i < customerList.size(); i++) {
			if (customerList.get(i).getCustomerSex() == 0) {
				manlist.add(customerList.get(i));
			} else {
				womanlist.add(customerList.get(i));
			}
		}
		for (int i = 0; i < clothesTypeList.size(); i++) {
			initSheetBook(staff, clothesTypeList.get(i), manlist, womanlist, hssfWorkbook);
		}
		return hssfWorkbook;
	}

	/**
	 * 导出汇总表
	 * 
	 * @param staff
	 *            当前登录帐号
	 * @param clothesTypeList
	 *            测量品类的集合
	 * @param customerList
	 *            用户测量数据
	 * @param hssfWorkbook
	 *            excelSheet 实例
	 */
	private void initTotleTable(Staff staff, List<ClothesType> clothesTypeList, List<CasdillyCustomer> customerList,
			XSSFWorkbook hssfWorkbook) {
		XSSFSheet sheet = hssfWorkbook.createSheet("草稿");
		Hashtable<String, List<ClothesTypeExport>> dict = fillTotleTableHeader(clothesTypeList, sheet);
		filltotleTableContext(staff, clothesTypeList, customerList, dict, sheet);
	}

	/**
	 * 填充导出汇总表表头
	 * 
	 * @param clothesTypeList
	 *            测量品类类型
	 * @param sheet
	 *            excelSheet 实例
	 * @return
	 */
	private Hashtable<String, List<ClothesTypeExport>> fillTotleTableHeader(List<ClothesType> clothesTypeList,
			XSSFSheet sheet) {
		int rowIndex = 0, columnIndex = 0;
		XSSFRow row = sheet.createRow(rowIndex);
		XSSFCell cell = row.createCell(columnIndex);
		cell.setCellValue("编号");
		columnIndex++;
		cell = row.createCell(columnIndex);
		cell.setCellValue("单位");
		columnIndex++;
		cell = row.createCell(columnIndex);
		cell.setCellValue("部门");
		columnIndex++;
		cell = row.createCell(columnIndex);
		cell.setCellValue("分部门");
		columnIndex++;
		cell = row.createCell(columnIndex);
		cell.setCellValue("班组");
		columnIndex++;
		cell = row.createCell(columnIndex);
		cell.setCellValue("岗位");
		columnIndex++;
		cell = row.createCell(columnIndex);
		cell.setCellValue("姓名");
		columnIndex++;
		cell = row.createCell(columnIndex);
		cell.setCellValue("性别");
		Hashtable<String, List<ClothesTypeExport>> dictionary = new Hashtable<String, List<ClothesTypeExport>>();
		for (int index = 0; index < clothesTypeList.size(); index++) {
			columnIndex++;
			cell = row.createCell(columnIndex);
			cell.setCellValue(clothesTypeList.get(index).getTypeName());
			columnIndex++;
			cell = row.createCell(columnIndex);
			cell.setCellValue(String.format("备注", clothesTypeList.get(index).getTypeName()));
			columnIndex++;
			cell = row.createCell(columnIndex);
			cell.setCellValue(String.format("可选款式", clothesTypeList.get(index).getTypeName()));
			List<ClothesTypeExport> clothesTypeExports = clothesTypeExportService
					.getClothesTypeExportByCategoryName(clothesTypeList.get(index).getTypeName());
			if (dictionary.containsKey(clothesTypeList.get(index).getTypeName())) {
				dictionary.remove(clothesTypeList.get(index).getTypeName());
				dictionary.put(clothesTypeList.get(index).getTypeName(), clothesTypeExports);
			} else {
				dictionary.put(clothesTypeList.get(index).getTypeName(), clothesTypeExports);
			}
			if (clothesTypeExports != null && clothesTypeExports.size() > 0) {
				XSSFCellStyle columnstyle = sheet.getWorkbook().createCellStyle();
				XSSFFont columnfont = sheet.getWorkbook().createFont();
				columnfont.setFontName("宋体");
				columnfont.setFontHeightInPoints((short) 12);
				columnstyle.setFont(columnfont);
				for (int i = 0; i < clothesTypeExports.size(); i++) {
					columnIndex++;
					if (clothesTypeExports.get(i).gettIsExportItemCorrectValue()
							&& clothesTypeExports.get(i).gettIsExportItemValue()) {
						cell = row.createCell(columnIndex);
						cell.setCellStyle(columnstyle);
						cell.setCellValue(clothesTypeExports.get(i).gettExportItemName());

						columnIndex++;
						cell = row.createCell(columnIndex);
						cell.setCellStyle(columnstyle);
						cell.setCellValue(String.format("%s修正", clothesTypeExports.get(i).gettExportItemName()));

					} else {
						cell = row.createCell(columnIndex);
						cell.setCellStyle(columnstyle);
						cell.setCellValue(clothesTypeExports.get(i).gettExportItemName());
					}
				}
			}
		}
		columnIndex++;
		cell = row.createCell(columnIndex);
		cell.setCellValue("量体");
		return dictionary;
	}

	private void filltotleTableContext(Staff staff, List<ClothesType> clothesTypeList,
			List<CasdillyCustomer> customerList, Hashtable<String, List<ClothesTypeExport>> dict, XSSFSheet sheet) {
		int rowIndex = sheet.getLastRowNum();
		for (int i = 0; i < customerList.size(); i++) {
			rowIndex++;
			XSSFRow row = sheet.createRow(rowIndex);
			filltotleTableRow(customerList.get(i), clothesTypeList, dict, row);
		}
	}

	private void filltotleTableRow(CasdillyCustomer casdillyCustomer, List<ClothesType> clothesTypeList,
			Hashtable<String, List<ClothesTypeExport>> dict, XSSFRow row) {
		int columnIndex = 0;
		XSSFCell cell = row.createCell(columnIndex);
		cell.setCellValue(casdillyCustomer.getCustomerIndex());
		columnIndex++;

		cell = row.createCell(columnIndex);
		cell.setCellValue(casdillyCustomer.getCustomerCom());
		columnIndex++;

		cell = row.createCell(columnIndex);
		cell.setCellValue(casdillyCustomer.getCustomerDep());
		columnIndex++;

		cell = row.createCell(columnIndex);
		cell.setCellValue(casdillyCustomer.getCustomerChildDep());
		columnIndex++;

		cell = row.createCell(columnIndex);
		cell.setCellValue(casdillyCustomer.getCustomerPost());
		columnIndex++;

		cell = row.createCell(columnIndex);
		cell.setCellValue(casdillyCustomer.getCustomerGroup());
		columnIndex++;

		cell = row.createCell(columnIndex);
		cell.setCellValue(casdillyCustomer.getCustomerName());
		columnIndex++;

		cell = row.createCell(columnIndex);
		cell.setCellValue(casdillyCustomer.getCustomerSex() == 0 ? "男" : "女");
		columnIndex++;
		XSSFCellStyle columnstyle = row.getSheet().getWorkbook().createCellStyle();
		XSSFFont columnfont = row.getSheet().getWorkbook().createFont();
		columnfont.setFontName("宋体");
		columnfont.setFontHeightInPoints((short) 12);
		columnstyle.setFont(columnfont);
		XSSFColor color = new XSSFColor();
		color.setRgb(new byte[] { 0, (byte) 127, (byte) 127 });
		columnstyle.setFillForegroundColor(color);
		for (int i = 0; i < clothesTypeList.size(); i++) {
			cell = row.createCell(columnIndex);
			cell.setCellValue(clothesTypeList.get(i).getQuantity());
			cell.setCellStyle(columnstyle);
			columnIndex++;
			cell = row.createCell(columnIndex);
			cell.setCellValue(casdillyCustomer.getBodyPartValue(clothesTypeList.get(i).getTypeName(), "备注"));
			cell.setCellStyle(columnstyle);
			columnIndex++;
			cell = row.createCell(columnIndex);
			cell.setCellValue(casdillyCustomer.getBodyPartValue(clothesTypeList.get(i).getTypeName(), "可选款式"));
			cell.setCellStyle(columnstyle);
			if (dict.containsKey(clothesTypeList.get(i).getTypeName())) {
				List<ClothesTypeExport> exports = dict.get(clothesTypeList.get(i).getTypeName());
				if (exports != null && exports.size() > 0) {
					for (int j = 0; j < exports.size(); j++) {
						if (exports.get(j).gettIsExportItemCorrectValue() && exports.get(j).gettIsExportItemValue()) {
							columnIndex++;
							cell = row.createCell(columnIndex);
							// cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
							getValueByExport(casdillyCustomer, exports.get(j).gettExportTypeName(),
									exports.get(j).gettExportItemName(), cell);

							columnIndex++;
							cell = row.createCell(columnIndex);
							getValueByExport(casdillyCustomer, exports.get(j).gettExportTypeName(),
									String.format("%s修正", exports.get(j).gettExportItemName()), cell);
						} else {
							columnIndex++;
							cell = row.createCell(columnIndex);
							// cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
							if (exports.get(j).gettIsExportItemValue()) {
								getValueByExport(casdillyCustomer, exports.get(j).gettExportTypeName(),
										exports.get(j).gettExportItemName(), cell);
							} else {
								getValueByExport(casdillyCustomer, exports.get(j).gettExportTypeName(),
										String.format("%s修正", exports.get(j).gettExportItemName()), cell);
							}
						}
					}
				}
			}
			columnIndex++;
		}
		cell = row.createCell(columnIndex);
		cell.setCellValue(casdillyCustomer.getV_uploaderName());
		cell.setCellStyle(columnstyle);
		columnIndex++;
	}

	private void getValueByExport(CasdillyCustomer casdillyCustomer, String categoryName, String bodyPartName,
			XSSFCell cell) {
		String value = "";
		String name = bodyPartName.replace("修正", "");
		casdillyCustomer.typeName = categoryName;
		if (bodyPartName.contains("号型") || bodyPartName.contains("规格")) {
			value = casdillyCustomer.getClothesSize();
		} else {
			value = casdillyCustomer.getBodyPartValue(categoryName, name);
			String[] array = value.split("_");
			if (array != null && array.length > 1) {
				if (bodyPartName.contains("修正")) {
					value = array[1];
				} else {
					value = array[0];
				}
				value = processValue(value);
			} else {
				value = processValue(value);
			}
		}
		cell.setCellValue(value);
	}

	/**
	 * 初始化sheet页
	 * 
	 * @param staff
	 * @param clothesType
	 * @param manlist
	 * @param womanlist
	 * @param hssfWorkbook
	 */
	private void initSheetBook(Staff staff, ClothesType clothesType, List<CasdillyCustomer> manlist,
			List<CasdillyCustomer> womanlist, XSSFWorkbook hssfWorkbook) {

		XSSFSheet sheetman = hssfWorkbook.createSheet(clothesType.getTypeName());
		if (manlist != null && manlist.size() > 0) {
			List<String> columnsman = initSheetHeader("男", 0, clothesType, manlist, sheetman);
			String ids = initSheetContext(staff, clothesType, manlist, columnsman, sheetman);
			initTotleData(ids, clothesType, sheetman);
		}
		if (womanlist.size() > 0) {
			int rowIndex = sheetman.getLastRowNum();
			if (manlist != null && manlist.size() > 0) {
				if (rowIndex > 2) {
					rowIndex++;
					sheetman.createRow(rowIndex);
					rowIndex++;
					sheetman.createRow(rowIndex);
				}
			}
			List<String> columns = initSheetHeader("女", ++rowIndex, clothesType, womanlist, sheetman);
			String ids = initSheetContext(staff, clothesType, womanlist, columns, sheetman);
			initTotleData(ids, clothesType, sheetman);
		}
	}

	/**
	 * 初始化表头
	 * 
	 * @param userSex
	 * @param rowIndex
	 * @param clothesType
	 * @param customerList
	 * @param sheet
	 * @return
	 */
	private List<String> initSheetHeader(String userSex, int rowIndex, ClothesType clothesType,
			List<CasdillyCustomer> customerList, XSSFSheet sheet) {
		List<String> list = new ArrayList<String>();
		XSSFRow row = sheet.createRow(rowIndex);
		XSSFCell cell = row.createCell(0);
		XSSFCellStyle style = sheet.getWorkbook().createCellStyle();
		XSSFFont font = sheet.getWorkbook().createFont();
		font.setFontName("宋体");
		font.setFontHeightInPoints((short) 18);
		style.setFont(font);
		style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
		int headerIndex = rowIndex;
		cell.setCellValue("广东浪登服装公司裁剪单");
		cell.setCellStyle(style);
		rowIndex++;
		if (customerList != null && customerList.size() > 0) {
			XSSFRow headRow = sheet.createRow(rowIndex);
			cell = headRow.createCell(0);
			cell.setCellValue(String.format("%s %s-%s 裁剪单", customerList.get(0).getCustomerCom(),
					clothesType.getTypeName(), userSex));
			cell.setCellStyle(style);
			rowIndex++;
		}

		XSSFCellStyle centerstyle = sheet.getWorkbook().createCellStyle();
		XSSFFont centerfont = sheet.getWorkbook().createFont();
		centerfont.setFontName("宋体");
		centerfont.setFontHeightInPoints((short) 16);
		centerstyle.setFont(centerfont);
		row = sheet.createRow(rowIndex);
		cell = row.createCell(0);
		cell.setCellStyle(centerstyle);
		cell.setCellValue(String.format("注：套码用\t，每人%s件\t\t", clothesType.getQuantity()));
		rowIndex++;

		XSSFCellStyle columnstyle = sheet.getWorkbook().createCellStyle();
		XSSFFont columnfont = sheet.getWorkbook().createFont();
		columnfont.setFontName("宋体");
		columnfont.setFontHeightInPoints((short) 12);
		columnstyle.setFont(columnfont);
		row = sheet.createRow(rowIndex);
		cell = row.createCell(0);
		cell.setCellStyle(columnstyle);
		cell.setCellValue("序号");
		cell = row.createCell(1);
		cell.setCellStyle(columnstyle);
		cell.setCellValue("编号");
		cell = row.createCell(2);
		cell.setCellStyle(columnstyle);
		cell.setCellValue("单位");
		cell = row.createCell(3);
		cell.setCellStyle(columnstyle);
		cell.setCellValue("姓名");
		cell = row.createCell(4);
		cell.setCellStyle(columnstyle);
		cell.setCellValue("性别");
		int columnIndex = 4;
		if (clothesType != null) {
			List<ClothesTypeExport> clothesTypeExports = clothesTypeExportService
					.getClothesTypeExportByCategoryName(clothesType.getTypeName());
			if (clothesTypeExports != null && clothesTypeExports.size() > 0) {
				for (int i = 0; i < clothesTypeExports.size(); i++) {
					columnIndex++;
					if (clothesTypeExports.get(i).gettIsExportItemCorrectValue()
							&& clothesTypeExports.get(i).gettIsExportItemValue()) {
						cell = row.createCell(columnIndex);
						cell.setCellStyle(columnstyle);
						cell.setCellValue(clothesTypeExports.get(i).gettExportItemName());
						list.add(clothesTypeExports.get(i).gettExportItemName());
						columnIndex++;
						cell = row.createCell(columnIndex);
						cell.setCellStyle(columnstyle);
						cell.setCellValue(String.format("%s修正", clothesTypeExports.get(i).gettExportItemName()));
						list.add(String.format("%s修正", clothesTypeExports.get(i).gettExportItemName()));
					} else {
						cell = row.createCell(columnIndex);
						cell.setCellStyle(columnstyle);
						cell.setCellValue(clothesTypeExports.get(i).gettExportItemName());
						if (clothesTypeExports.get(i).gettIsExportItemValue()) {
							list.add(clothesTypeExports.get(i).gettExportItemName());
						} else {
							list.add(String.format("%s修正", clothesTypeExports.get(i).gettExportItemName()));
						}
					}
				}
			}
			columnIndex++;
			cell = row.createCell(columnIndex);
			cell.setCellStyle(columnstyle);
			cell.setCellValue("备注");
			list.add("备注");
			columnIndex++;
			cell = row.createCell(columnIndex);
			cell.setCellStyle(columnstyle);
			cell.setCellValue("可选款式");
			columnIndex++;
			cell = row.createCell(columnIndex);
			cell.setCellStyle(columnstyle);
			cell.setCellValue("量体");
			sheet.addMergedRegion(new CellRangeAddress(headerIndex, headerIndex, 0, columnIndex));
			sheet.addMergedRegion(new CellRangeAddress(headerIndex + 1, headerIndex + 1, 0, columnIndex));
		}
		return list;
	}

	/**
	 * 初始化sheet数据
	 * 
	 * @param staff
	 * @param clothesType
	 * @param customerList
	 * @param columns
	 * @param sheet
	 * @return
	 */
	private String initSheetContext(Staff staff, ClothesType clothesType, List<CasdillyCustomer> customerList,
			List<String> columns, XSSFSheet sheet) {
		StringBuilder dataStr = new StringBuilder();
		if (customerList != null && customerList.size() > 0) {
			CasdillyCustomer.typeName = clothesType.getTypeName();
			CasdillyCustomer[] list = CasdillyCustomer.OrderByCloseSize(customerList);
			int rowIndex = sheet.getLastRowNum();
			for (CasdillyCustomer casdillyCustomer : list) {
				dataStr.append(String.format("'%s',", casdillyCustomer.getCustomerID()));
				rowIndex++;
				XSSFRow row = sheet.createRow(rowIndex);
				initSheetRow(staff, clothesType, casdillyCustomer, columns, row);
			}
		}
		return dataStr.toString();
	}

	/**
	 * 初始化数据行
	 * 
	 * @param staff
	 *            当前登录帐号
	 * @param clothestype
	 *            品类信息
	 * @param casdillyCustomer
	 *            测量用户
	 * @param columns
	 *            该数据行的列集合
	 * @param row
	 *            数据行
	 */
	private void initSheetRow(Staff staff, ClothesType clothestype, CasdillyCustomer casdillyCustomer,
			List<String> columns, XSSFRow row) {
		casdillyCustomer.typeName = clothestype.getTypeName();
		XSSFCellStyle columnstyle = row.getSheet().getWorkbook().createCellStyle();
		XSSFFont columnfont = row.getSheet().getWorkbook().createFont();
		columnfont.setFontName("宋体");
		columnfont.setFontHeightInPoints((short) 12);
		columnstyle.setFont(columnfont);
		XSSFCell cell = row.createCell(0);
		cell.setCellValue(casdillyCustomer.getCustomerID());
		cell.setCellStyle(columnstyle);
		cell = row.createCell(1);
		cell.setCellStyle(columnstyle);
		cell.setCellValue(casdillyCustomer.getCustomerIndex());
		cell = row.createCell(2);
		cell.setCellValue(casdillyCustomer.getCustomerCom());
		cell.setCellStyle(columnstyle);
		cell = row.createCell(3);
		cell.setCellValue(casdillyCustomer.getCustomerName());
		cell.setCellStyle(columnstyle);
		cell = row.createCell(4);
		cell.setCellValue(casdillyCustomer.getCustomerSex() == 0 ? "男" : "女");
		cell.setCellStyle(columnstyle);
		int columnIndex = 4;
		if (columns != null && columns.size() > 0) {
			for (int i = 0; i < columns.size(); i++) {
				columnIndex++;
				cell = row.createCell(columnIndex);
				getValueByExport(casdillyCustomer, clothestype.getTypeName(), columns.get(i), cell);
				cell.setCellStyle(columnstyle);
			}
		}

		columnIndex++;
		cell = row.createCell(columnIndex);
		cell.setCellValue(casdillyCustomer.getBodyPartValue(clothestype.getTypeName(), "可选款式"));
		columnIndex++;
		cell = row.createCell(columnIndex);
		cell.setCellValue(casdillyCustomer.getV_uploaderName());
		cell.setCellStyle(columnstyle);
	}

	private String processValue(String value) {
		String str = "";
		if (value != null && !value.equals("0") && !value.equals("0.0")) {
			str = value;
		}
		return str;
	}

	/**
	 * 初始化号型汇总数据
	 * 
	 * @param ids
	 * @param clothesType
	 * @param sheet
	 */
	private void initTotleData(String ids, ClothesType clothesType, XSSFSheet sheet) {
		if (ids != null && ids.length() > 0) {
			XSSFCellStyle columnstyle = sheet.getWorkbook().createCellStyle();
			XSSFFont columnfont = sheet.getWorkbook().createFont();
			columnfont.setFontName("宋体");
			columnfont.setFontHeightInPoints((short) 12);
			columnstyle.setFont(columnfont);
			ids = ids.substring(0, ids.length() - 1);
			CasdillyScanData scanData = new CasdillyScanData();
			scanData.setCategory(clothesType.getTypeName());
			scanData.setShuzhi(ids);
			List<CasdillyScanData> datas = casdillyScanDataService.getScanDataTotole(scanData);
			int rowIndex = sheet.getLastRowNum();
			rowIndex++;
			XSSFRow row = sheet.createRow(rowIndex);
			XSSFCell cell = row.createCell(0);
			cell.setCellValue("规格汇总---");
			cell.setCellStyle(columnstyle);
			rowIndex++;
			XSSFRow headrow = sheet.createRow(rowIndex);
			cell = headrow.createCell(0);
			cell.setCellStyle(columnstyle);
			cell.setCellValue("规格");

			rowIndex++;
			XSSFRow datarow = sheet.createRow(rowIndex);
			cell = datarow.createCell(0);
			cell.setCellValue("数量");
			cell.setCellStyle(columnstyle);
			int columnIndex = 1;
			long totle = 0;
			long count = 0;
			if (datas != null && datas.size() > 0) {
				for (int i = 0; i < datas.size(); i++) {
					count = datas.get(i).getId() * StringUtils.tryParseInt(clothesType.getQuantity());
					cell = headrow.createCell(columnIndex);
					cell.setCellStyle(columnstyle);
					cell.setCellValue(datas.get(i).getShuzhi());
					cell = datarow.createCell(columnIndex);
					cell.setCellValue(count);
					cell.setCellStyle(columnstyle);
					totle += count;
					columnIndex++;
				}
			}
			cell = headrow.createCell(columnIndex);
			cell.setCellStyle(columnstyle);
			cell.setCellValue("总数");

			cell = datarow.createCell(columnIndex);
			cell.setCellValue(totle);
			cell.setCellStyle(columnstyle);
			columnIndex++;
			rowIndex++;
			datarow = sheet.createRow(rowIndex);
			cell = datarow.createCell(0);
			cell.setCellValue("制表:          ");
			cell.setCellStyle(columnstyle);
			cell = datarow.createCell(1);
			cell.setCellStyle(columnstyle);
			cell = datarow.createCell(2);
			cell.setCellStyle(columnstyle);
			cell = datarow.createCell(3);
			cell.setCellStyle(columnstyle);
			cell = datarow.createCell(4);
			cell.setCellValue("业务:          ");
			cell.setCellStyle(columnstyle);
			cell = datarow.createCell(5);
			cell.setCellStyle(columnstyle);
			cell = datarow.createCell(6);
			cell.setCellStyle(columnstyle);
			cell = datarow.createCell(7);
			cell.setCellStyle(columnstyle);
			cell = datarow.createCell(8);
			cell.setCellValue("日期:          ");
			cell.setCellStyle(columnstyle);
		}
	}
}
